﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ZX.WebApp.Message
{
    public partial class gvpager : System.Web.UI.UserControl
    {
        protected GridView ctlGridView = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnGoto_Click(object sender, EventArgs e)
        {
            if (this.FindControl("txtGoTo").Parent.Parent.Parent.Parent.Parent is GridView)
            {
                ctlGridView = this.FindControl("txtGoTo").Parent.Parent.Parent.Parent.Parent as GridView;
                PageChanged(sender, ctlGridView.BottomPagerRow);
            }
        }

        public void PageChanged(object sender, GridViewRow PagerRow)
        {
            if (PagerRow != null)
            {
                TextBox txtGoTo = (TextBox)((Button)sender).Parent.FindControl("txtGoTo");
                int iGoTo = ToInt(txtGoTo.Text.Trim(), -1);
                if (iGoTo > 0)
                {
                    if (iGoTo > ctlGridView.PageCount)
                    {
                        ctlGridView.PageIndex = ctlGridView.PageCount - 1;
                    }
                    else
                    {
                        ctlGridView.PageIndex = iGoTo - 1;
                    }
                }
            }
        }

        public int ToInt(object o, int defaultValue)
        {
            if (o == null)
            {
                return defaultValue;
            }
            int.TryParse(o.ToString(), out defaultValue);
            return defaultValue;
        }
    }
}